<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>玩家3倒计时</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.1/socket.io.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            text-align: center;
            margin: 0;
            padding: 20px;
            background-color: #ffebee;
        }

        .container {
            max-width: 400px;
            margin: 0 auto;
            background-color: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            border: 4px solid #e74c3c;
        }

        .timer-display {
            font-size: 72px;
            margin: 20px 0;
            color: #e74c3c;
        }

        .player-name {
            font-size: 24px;
            color: #e74c3c;
            font-weight: bold;
        }

        button {
            padding: 15px 30px;
            margin: 10px;
            font-size: 18px;
            cursor: pointer;
            background-color: #e74c3c;
            color: white;
            border: none;
            border-radius: 5px;
            width: 80%;
        }

        button:hover {
            background-color: #c0392b;
        }

        .active {
            box-shadow: 0 0 15px gold;
        }
    </style>
</head>

<body>
    <div class="container" id="container">
        <div class="player-name">玩家3</div>
        <div class="timer-display" id="timer">03:00</div>
        <button id="pause-btn">暂停并切换玩家</button>
    </div>

    <audio id="alarm-sound" src="https://assets.mixkit.co/sfx/preview/mixkit-alarm-digital-clock-beep-989.mp3"
        preload="auto"></audio>

    <script>
        const socket = io();
        const player_id = 3; // 当前玩家ID

        // 格式化时间为MM:SS
        function formatTime(seconds) {
            const mins = Math.floor(seconds / 60);
            const secs = seconds % 60;
            return `${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
        }

        // 更新显示
        function updateDisplay(data) {
            const myTime = data.players[player_id].remaining_time;
            document.getElementById('timer').textContent = formatTime(myTime);

            // 如果是当前玩家，高亮显示
            if (player_id === data.current_player) {
                document.getElementById('container').classList.add('active');
            } else {
                document.getElementById('container').classList.remove('active');
            }
        }

        // 播放提示音
        function playAlarm() {
            const alarm = document.getElementById('alarm-sound');
            alarm.currentTime = 0;
            alarm.play();
        }

        // 暂停按钮事件
        document.getElementById('pause-btn').addEventListener('click', () => {
            console.log('pause')
            socket.emit('pause_and_switch');
        });

        // Socket.IO 事件监听
        socket.on('game_update', (data) => {
            updateDisplay(data);
        });

        socket.on('player_switched', (data) => {
            updateDisplay(data);
        });

        socket.on('game_reset', (data) => {
            updateDisplay(data);
        });

        socket.on('timer_end', (data) => {
            if (data.player === player_id) {
                playAlarm();
                alert('您的时间到了！');
            }
        });
    </script>
</body>

</html>